﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_ToolEdit : DevExpress.XtraEditors.XtraForm
	{
		public string ToolId;
		public string ToolName = "";
		public string MakerToolName = "";
        public decimal Price;
		public int SafeQty;
		public string FrequencyId = "";
		public string ToolGroupId = "";
		public string MakerId = "";
		public int ToolOrQTool = 0;
		private ucTool parent;

		public frm_ToolEdit(ucTool fTool)
		{
			InitializeComponent();
			parent = fTool;
			this.CancelButton = this.btCancel;
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			if(GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_ToolEdit_Load(object sender, EventArgs e)
		{
			BindDataToMaker();
			BindData();
			BindDataToFrequency();
			BindDataToToolGroup();
			if (ToolOrQTool == ToolConstants.Configuration.ToolType.IsTool)
			{
				rdTool.Checked = true;
				cmbMakerID.Enabled = false;
			}
			else
			{
				rdQTool.Checked = true;
				cmbMakerID.Enabled = true;
			}

			
		}

		private void rdTool_CheckedChanged(object sender, EventArgs e)
		{
			cmbMakerID.SelectedItem = cmbMakerID.Items[0];
			cmbMakerID.Enabled = false;
		}

		private void rdQTool_CheckedChanged(object sender, EventArgs e)
		{
			cmbMakerID.Enabled = true;
		}

		private void txtToolID_KeyPress(object sender, KeyPressEventArgs e)
		{

		}

		//Private methods
		#region Private methods

		private bool CheckDuplicate(string id)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			helper.ValueObject.ToolID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}

		private bool GetData(Tools o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtToolID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtToolName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtMakerToolname.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.MakerToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtSafeQty.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.SafeQtyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(rdQTool.Checked && Utilities.IsNullOrEmpty(cmbMakerID.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.MakerCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbFrequency.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.FrequencyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbToolGroup.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolGroupCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			if(string.Compare(txtToolID.Text.Trim(), ToolId) != 0)
			{
				if (CheckDuplicate(txtToolID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.Tool.ToolHasExisted, ToolConstants.Form.MessageBox.Error,
					                MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.TempId = txtToolID.Text;
			o.ToolID = ToolId;
			o.ToolName = txtToolName.Text;
			o.MakerToolName = txtMakerToolname.Text;
            o.Price = decimal.Parse(txtPrice.Text);
			o.SafeQty = int.Parse(txtSafeQty.Text);
			o.FrequencyID = cmbFrequency.SelectedValue.ToString();
			o.ToolGroupID = cmbToolGroup.SelectedValue.ToString();
			o.MakerID = cmbMakerID.SelectedValue.ToString();
			if(rdTool.Checked)
			{
				o.ToolOrQTool = ToolConstants.Configuration.ToolType.IsTool;
			}
			else
			{
				o.ToolOrQTool = ToolConstants.Configuration.ToolType.IsQTool;

			}
			return true;
		}

		private void BindData()
		{
			txtToolID.Text = ToolId;
			txtToolName.Text = ToolName;
			txtMakerToolname.Text = MakerToolName;
			txtPrice.Text = Price.ToString();
			txtSafeQty.Text = SafeQty.ToString();
		}

		private void BindDataToFrequency()
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbFrequency.DataSource = dt;
            cmbFrequency.DisplayMember = ToolConstants.Entities.Frequency.FieldName.FullName;
			cmbFrequency.ValueMember = ToolConstants.Entities.Frequency.FieldName.FrequencyID;
            row[ToolConstants.Entities.Frequency.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Frequency.FieldName.FrequencyID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbFrequency.SelectedValue = FrequencyId;
			if(cmbFrequency.SelectedItem == null)
			{
				cmbFrequency.SelectedItem = cmbFrequency.Items[0];
			}
		}

		private void BindDataToToolGroup()
		{
			ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbToolGroup.DataSource = dt;
            cmbToolGroup.DisplayMember = ToolConstants.Entities.ToolGroup.FieldName.FullName;
			cmbToolGroup.ValueMember = ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID;
            row[ToolConstants.Entities.ToolGroup.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbToolGroup.SelectedValue = ToolGroupId;
			if(cmbToolGroup.SelectedItem == null)
			{
				cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];
			}
		}

		private void BindDataToMaker()
		{
			MakerHelper helper = new MakerHelper(new Maker());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbMakerID.DataSource = dt;
            cmbMakerID.DisplayMember = ToolConstants.Entities.Maker.FieldName.FullName;
			cmbMakerID.ValueMember = ToolConstants.Entities.Maker.FieldName.MakerID;
            row[ToolConstants.Entities.Maker.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Maker.FieldName.MakerID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbMakerID.SelectedItem = MakerId;
			if(cmbMakerID.SelectedItem == null)
			{
				cmbMakerID.SelectedItem = cmbMakerID.Items[0];
			}
		}
		#endregion



	}
}